package cn.eoeit.mmy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.eoeit.mmy.bean.Pagination;
import cn.eoeit.mmy.bean.RespCode;
import cn.eoeit.mmy.bean.Response;
import cn.eoeit.mmy.model.FeedBackModel;
import cn.eoeit.mmy.model.RoleModel;
import cn.eoeit.mmy.model.UserInfo;
import cn.eoeit.mmy.service.ISessionService;
import cn.eoeit.mmy.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private ISessionService sessionService;

	@RequestMapping("/userlist.action")
	public String userlist(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectAllUsers(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "userList";
	}

	@RequestMapping("/userToAdd.action")
	public String userToAdd(ModelMap modelMap) {
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		return "userAdd";
	}
	
	@RequestMapping("/userToUpdate.action")
	public String userToUpdate(Integer uid ,ModelMap modelMap) {
		UserInfo userInfo=userService.findUserInfo(uid);
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		modelMap.addAttribute("userInfo", userInfo);
		return "userUpdate";
	}
	
	
	@RequestMapping("/updateUser.action")
	public  @ResponseBody Response updateUser(@RequestBody UserInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		UserInfo userInfo =userService.finishUpdateUser(user);
		if(user!=null){
			response.setMessage("用户添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("用户添加失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/addUser.action")
	public  @ResponseBody Response addUser(@RequestBody UserInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =userService.finishAddUserInfo(user);
		if(user!=null){
			response.setMessage("用户添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("用户添加失败"); 
			
		} 
		
		return response;
	}
	@RequestMapping("/deleteUser.action")
	public  @ResponseBody Response deleteUser(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		UserInfo user =userService.finishDelete(uid);
		if(user!=null){
			response.setMessage("用户添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("用户添加失败"); 
			
		} 
		
		return response;
	}
	


	@RequestMapping("/feedbackToAdd.action")
	public String feedbackToAdd(ModelMap modelMap) {
		
		return "feedbackToAdd";
	}
	
	
	@RequestMapping("/addFeedBack.action")
	public  @ResponseBody Response addFeedBack(@RequestBody FeedBackModel feedBackModel,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		UserInfo userInfo=sessionService.getLoginInfo(request);
		feedBackModel.setFeedUserId(userInfo.getId());
		feedBackModel =userService.finishAddFeeBack(feedBackModel);
		if(feedBackModel!=null){
			response.setMessage("质量反馈添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("质量反馈添加失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/feedBacklist.action")
	public String feedBacklist(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectFeedBacks(page, 0);
		modelMap.addAttribute("pagination", userInfos);
		return "feedBackList";
	}

	
	
	
	@RequestMapping("/showInfos")
	public @ResponseBody
	Object showUserInfos() {
		List<UserInfo> userInfos = userService.getUsers();
		return userInfos;
	}
}